package com.qc31.monitorlib.ui.carinfo

import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.qc31.baselib.base.LazyFragment
import com.qc31.comlib.Keys
import com.qc31.comlib.entity.NameValue
import com.qc31.monitorlib.R
import com.qc31.monitorlib.databinding.FragmentSignBinding

/** @Author Jasper @Time 2021/9/4 15:16 @Desc 信号线 */
class SignRemindFragment : LazyFragment<FragmentSignBinding>(FragmentSignBinding::inflate) {
    private val mViewModel by activityViewModels<CarInfoViewModel>()
    private var type: String = "0"

    companion object {
        fun instance(type: String): Fragment {
            val fragment = SignRemindFragment()
            val args = Bundle()
            args.putString(Keys.INTENT_OTHER, type)
            fragment.arguments = args
            return fragment
        }
    }

    private val signAdapter by lazy { SignalAdapter() }
    private val remindAdapter by lazy { RemindAdapter() }

    override fun initView(view: View) {
        type = arguments?.getString(Keys.INTENT_OTHER) ?: "0"
        binding.recyclerRS.layoutManager = LinearLayoutManager(requireContext())
        if (type == "0") {
            binding.recyclerRS.adapter = signAdapter
            binding.llSignal.isVisible = true
            binding.llRemind.isVisible = false
//            signAdapter.addHeaderView(HeadviewSgnBinding.inflate(layoutInflater).root)
        } else {
            binding.llSignal.isVisible = false
            binding.llRemind.isVisible = true
            binding.recyclerRS.adapter = remindAdapter
//            remindAdapter.addHeaderView(HeadviewRemindBinding.inflate(layoutInflater).root)
        }
    }

    override fun initData() {
        mViewModel.getSgnRemind(type)
        if (type == "0")
            mViewModel.sgnLive.observe(this) {
                signAdapter.setList(it)
            }
        else
            mViewModel.remindLive.observe(this) {
                remindAdapter.setList(it)
            }
    }

    override fun initEvent() {

    }

    inner class RemindAdapter :
        BaseQuickAdapter<NameValue, BaseViewHolder>(R.layout.item_remind) {
        override fun convert(holder: BaseViewHolder, item: NameValue) {
            item.run {
                val plus = value.split(",")
                holder.setText(R.id.tvSRNum, (holder.layoutPosition + 1).toString())
                holder.setText(R.id.tvSRTitle, name)

                if (plus.size > 1) {
                    holder.setText(R.id.tvSRDate, plus[0])
                    holder.setText(R.id.tvSROver, plus[1])
                } else {
                    holder.setText(R.id.tvSRDate, plus[0])
                    holder.setText(R.id.tvSROver, "")
                }
            }
        }
    }

    inner class SignalAdapter :
        BaseQuickAdapter<NameValue, BaseViewHolder>(R.layout.item_signal) {
        override fun convert(holder: BaseViewHolder, item: NameValue) {
            item.run {
                holder.setText(R.id.tvSRNum, (holder.layoutPosition + 1).toString())
                holder.setText(R.id.tvSRTitle, name)
                holder.setText(R.id.tvSRValue, value)
            }
        }
    }
}